const mysql = require('mysql2/promise');

// 请根据实际情况修改数据库配置
const pool = mysql.createPool({
    host: 'localhost',
    user: 'username',
    password: 'password',
    database: 'black',
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0
});

async function initUserTable() {
    const createTableSQL = `CREATE TABLE IF NOT EXISTS \`user\` (
    \`id\` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
    \`username\` VARCHAR(50) NOT NULL UNIQUE COMMENT '用户名',
    \`phone\` VARCHAR(20) NOT NULL UNIQUE COMMENT '手机号',
    \`password\` VARCHAR(255) NOT NULL COMMENT '密码（加密存储）',
    \`avatar\` VARCHAR(255) DEFAULT NULL COMMENT '头像URL',
    \`extra\` JSON DEFAULT NULL COMMENT '扩展字段',
    \`created_at\` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    \`updated_at\` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (\`id\`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';`;
    const conn = await pool.getConnection();
    try {
        await conn.query(createTableSQL);
    } finally {
        conn.release();
    }
}

module.exports = pool;
module.exports.initUserTable = initUserTable; 